/*
 * Copyright (C) Francisco Morero Peyrona. All rights reserved.
 *
 * This software is published under the terms of Open Source
 * License version 1.1, a copy of which has been included with this
 * distribution in the License.txt file.
 */

package jdbcmanager.dbtools.tableditor;

import javax.swing.*;

import jdbcmanager.dbtools.datatypes.*;

/**
 * A JTable to show ONE at a time of the indices that are attached to a DB Table.
 * It also has functionality to add, remove, edit and shift index columns (criteria).
 *
 * @author Francisco Morero Peyrona
 */
final class JxTableIndices extends JxTableParent
{
   private final static int COL_KEY    = 0;
   private final static int COL_UNIQUE = 1;
   private final static int COL_SORT   = 2;
   
   private JComboBox cmbKey;     // Cell editor for Key  column
   private JComboBox cmbSort;    // Cell editor for Sort column
   
   //-------------------------------------------------------------------------//
   
   /**
    * Class constructor 
    */
   JxTableIndices()
   {      
      setModel( new IndexTableModel() );
      setHead( new String[] { "Key", "Unique", "Sort" } );
      init();
   }
   
   /**
    * @see jdbcmanager.dbtools.tableditor.JxTableParent#createEmptyRow()
    */
   protected Object createEmptyRow()
   {
      return new IndexColumnDefinition();
   }
   
   /**
    * 
    * @param array
    */
   void setColumns( IndexColumnDefinition[] array )
   {
      setData( array );
   }
   
   /**
    * 
    * @return
    */
   IndexColumnDefinition[] getColumns()
   {
      IndexColumnDefinition[] aRet = new IndexColumnDefinition[ this.vData.size() ];

      this.vData.copyInto( aRet );
      
      return aRet;
   }
   
   /**
    * 
    * @param asColName
    */
   void setAvailableColumNames( String[] asColName )
   {      
      this.cmbKey.removeAllItems();
      
      for( int n = 0; n < asColName.length; n++ )
         this.cmbKey.addItem( asColName[n] );
      
      // If previously selected item (key column name) does not exists anymore: set it to null
      IndexColumnDefinition[] aICD = getColumns();
      
      for( int n = 0; n < aICD.length; n++ )
      {
         String sColumn = aICD[n].getName(); 
         int    nIndex  = ((DefaultComboBoxModel) this.cmbKey.getModel()).getIndexOf( sColumn );
         
         if( nIndex < 0 )
            aICD[n].setName( "" );
      }
   }
   
   //-------------------------------------------------------------------------//

   private void init()
   {      
      // Sets ComboBox to edit Column Name  ---------------------------
      this.cmbKey = new JComboBox();
      this.cmbKey.setEditable( true );
      
      (getColumnModel().getColumn( COL_KEY )).setCellEditor( new DefaultCellEditor( this.cmbKey ) );

      // Sets ComboBox to edit Ascending / Descending  ---------------------------
      this.cmbSort = new JComboBox( new String[] { "Ascending", "Descending" } );
      this.cmbSort.setSelectedIndex( 0 );
      
      (getColumnModel().getColumn( COL_SORT )).setCellEditor( new DefaultCellEditor( this.cmbSort ) );
      //---------------------------------------------------------------
      
      getColumnModel().getColumn( COL_KEY    ).setPreferredWidth( 220 );
      getColumnModel().getColumn( COL_UNIQUE ).setPreferredWidth(  45 );
      getColumnModel().getColumn( COL_SORT   ).setPreferredWidth(  90 );
   }

   //---------------------------------------------------------------//
   // INNER CLASS: TABLE MODEL
   //---------------------------------------------------------------//
   private final class IndexTableModel extends JxTableParent.ParentTableModel
   {
      public Object getValueAt( int nRow, int nCol )
      {
         IndexColumnDefinition column = (IndexColumnDefinition) JxTableIndices.this.vData.get( nRow );
         Object oRet;

         switch( nCol )
         {
            case COL_KEY   :  oRet = column.getName();                                                       break;
            case COL_UNIQUE:  oRet = new Boolean( column.isUnique() );                                       break;
            case COL_SORT  :  oRet = JxTableIndices.this.cmbSort.getItemAt( column.isAscending() ? 0 : 1 );  break;
            default: oRet = "<Error>";  // Avoid to create this String instance if there is no error
         }
         
         return oRet;
      }

      public void setValueAt( Object value, int nRow, int nCol )
      {
         IndexColumnDefinition column = (IndexColumnDefinition) JxTableIndices.this.vData.get( nRow );

         switch( nCol )
         {
            case COL_KEY   : column.setName(      (String) value );                                      break;
            case COL_UNIQUE: column.setUnique(    ((Boolean) value).booleanValue() );                    break;
            case COL_SORT  : column.setAscending( JxTableIndices.this.cmbSort.getSelectedIndex() == 0 ); break;
         }
      }
   } // End of InnerClass: Table Model
}